// 分配角色组件
<template>
  <div>
    <!-- 上面是角色渲染 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in rolelist" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>
    <!-- 点击确定和取消按钮 -->
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addRole">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    useId: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 选中的id
      rolelist: []// 所有角色的列表
    }
  },
  created() {
    this.getRoles()
    this.getUserDetailById()
  },
  methods: {
    // 点击取消按钮
    cancleDialog() {
      this.$emit('close')
    },
    // 获取所有角色的列表
    async getRoles() {
      const res = await getRoles()

      this.rolelist = res.data.rows
    },
    // 获取角色所有选中的
    async getUserDetailById() {
      const res = await getUserDetailById(this.useId)
      //   console.log(res)
      this.roleIds = res.data.roleIds
    },
    // 点击确定
    async addRole() {
    //   console.log(this.roleIds)
      const res = await assignRoles({ id: this.useId, roleIds: this.roleIds })
      //   console.log(res)
      this.$message.success(res.message)
      //   this.getUserDetailById()// 重新获取选中的列表的数据
      this.$emit('close')
      // 重新获取当前当前列表数据
    //   this.getRoles()
    //   this.getUserDetailById()//
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
